package com.lmh.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lmh.entity.User;
import com.lmh.mapper.UserDao;
import com.lmh.utils.Maps;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserService {

    @Autowired
    private UserDao userDao;

    public int create(User user) {
        return userDao.create(user);
    }

    public int delete(Integer id) {
        // Maps是自定义的工具类，支持链式封装
        return userDao.delete(Maps.build(id).getParamMap());
    }

    // 构建Map参数时需要先将原生id放入map中，否则会导致全部更新
    public int update(User user) {
        return userDao.update(Maps.build(user.getId()).beanToMapForUpdate(user));
    }

    public PageInfo<User> query(User user) {
        if(user != null && user.getPage() != null) {
            PageHelper.startPage(user.getPage(), user.getLimit());
        }
        List<User> list = userDao.query(Maps.build().beanToMap(user));
        return new PageInfo<User>(list);
    }

    public User detail(Integer id) {
        return userDao.detail(Maps.build(id).getParamMap());
    }

    public User login(String userName, String password) {
        return userDao.detail(Maps.build()
                .add("userName", userName)
                .add("password", password)
                .getParamMap());
    }



    public int count(User user) {
        return userDao.count(Maps.build().beanToMap(user));
    }

}
